<?php

/**
 * This page can be requested by the browser and will output all the currently online
 * servers in a preset format (the servers are split by a & sign, the data in the servers
 * is split by a | sign). It also checks every 10 seconds whether or not we received a ping
 * from the servers in the last 10 seconds. If not, we consider the server offline and remove
 * it from the masterlist.
 * 
 * @author Matthias Van Eeghem <matthias@van-eeghem.com>
 * @copyright Copyright (c) 2012, Matthias Van Eeghem
 */

// We need these defines for MySQL connections
require_once('Defines.php');
// Remove all invalid servers from the masterlist before we return it to the client
require_once('InvalidServerCheck.php');

// Open a MySQL connection
$MySQLi = new mysqli(MySQL_Server, MySQL_User, MySQL_Password, MySQL_Database);

// EO checking for offline servers on the masterlist
// Start of the actual outputting of the online servers
// First we select all data in the MasterList table
$preparedStatement = $MySQLi->prepare("SELECT IP, Version, Hostname, OnlinePlayers FROM MasterList");
// Bind the result to variables
$preparedStatement->bind_result($sIP, $sVersion, $sHostname, $iOnlinePlayers);

// Execute the query
if($preparedStatement->execute())
{
	while($preparedStatement->fetch())
	{
		// Simply echo all server information, the browser knows how to process this properly
		// All servers are split by a & character, and the individual data of the servers is split by the | character
		echo $sIP . '-' . $sVersion . '-' . $sHostname . '-' . $iOnlinePlayers . PHP_EOL;
	}
}

// Don't forget to close the statement
$preparedStatement->close();
?>